package pattern;

/**
 * Created with IntelliJ IDEA.
 * Description:实现阻塞队列
 * User: 东莞呵呵
 * Date:2022-09-19
 * Time:17:45
 */
class MyBlockingDeque{
    private int[] elem=new int[1000];
    private int head=0;
    private int tail=0;
    private int size=0;

    public void put(int data) throws InterruptedException {
        synchronized (this){
            while (this.size==this.elem.length){
                this.wait();
            }
            this.elem[this.tail++]=data;
            if(this.tail>=this.elem.length){
                this.tail=0;
            }
            this.size++;
            this.notify();
        }
    }

    public int take() throws InterruptedException {
        while (this.size==0){
            this.wait();
        }
        int ret=this.elem[head++];
        if(this.head>=this.elem.length){
            this.head=0;
        }
        this.size--;
        this.notify();
        return ret;
    }


}
public class Demo3 {
    public static void main(String[] args) {

    }
}
